package com.ctg.behavior.calc.impala.builder.addictions;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.common.CtCalcDate;
import com.ctg.behavior.calc.impala.report.addictions.CtAddictionsTimesReport;
import com.ctg.behavior.calc.utils.CtCalcDaysUtils;
import com.ctg.behavior.common.addictions.CtAddictionsCalcBody;
import com.ctg.behavior.common.consts.CtCalcConsts;
import com.ctg.behavior.utils.CtDateUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CtAddictionsTimesReportBuilder {

	public List<CtAddictionsTimesReport> doBuilder(CtAddictionsCalcBody calcBody) {

		log.info("calcBody={}", JSON.toJSONString(calcBody));

		List<CtAddictionsTimesReport> calcReports = new ArrayList<CtAddictionsTimesReport>();

		if (!CtCalcConsts.MEASURE_TYPE_TIMES.equals(calcBody.getMeasureType())) {
			log.error("measureType={}", calcBody.getMeasureType());
			return calcReports;
		}
		String unit = calcBody.getUnit();
		if (CtCalcConsts.UNIT_TYPE_DAY.equals(unit)) {
			CtAddictionsTimesReport oneReport = this.copyBody(calcBody);
			oneReport.setTheCalcFromDate(calcBody.getFromDate());
			oneReport.setTheCalcToDate(calcBody.getToDate());
			if (calcBody.getResultBucketParam().isEmpty()) {
				oneReport.getBuckets().add("3");
				oneReport.getBuckets().add("5");
				oneReport.getBuckets().add("10");
			} else {
				oneReport.setBuckets(calcBody.getResultBucketParam());
			}
			calcReports.add(oneReport);
		} else if (CtCalcConsts.UNIT_TYPE_WEEK.equals(unit)) {
			List<CtCalcDate> weekDates = null;
			weekDates = CtCalcDaysUtils.listCalcWeeks(calcBody.getFromDate(), calcBody.getToDate());
			for (CtCalcDate weekDate : weekDates) {
				CtAddictionsTimesReport oneReport = this.copyBody(calcBody);
				oneReport.setTheCalcFromDate(CtDateUtils.dateToString(weekDate.getFromDate(), CtDateUtils.YYYY_MM_DD));
				oneReport.setTheCalcToDate(CtDateUtils.dateToString(weekDate.getToDate(), CtDateUtils.YYYY_MM_DD));

				if (calcBody.getResultBucketParam().isEmpty()) {
					oneReport.getBuckets().add("3");
					oneReport.getBuckets().add("5");
					oneReport.getBuckets().add("10");
				} else {
					oneReport.setBuckets(calcBody.getResultBucketParam());
				}

				calcReports.add(oneReport);
			}
		} else if (CtCalcConsts.UNIT_TYPE_MONTH.equals(unit)) {
			List<CtCalcDate> monthDates = null;
			monthDates = CtCalcDaysUtils.listCalcMonths(calcBody.getFromDate(), calcBody.getToDate());
			for (CtCalcDate monthDate : monthDates) {
				CtAddictionsTimesReport oneReport = this.copyBody(calcBody);
				oneReport.setTheCalcFromDate(CtDateUtils.dateToString(monthDate.getFromDate(), CtDateUtils.YYYY_MM_DD));
				oneReport.setTheCalcToDate(CtDateUtils.dateToString(monthDate.getToDate(), CtDateUtils.YYYY_MM_DD));

				if (calcBody.getResultBucketParam().isEmpty()) {
					oneReport.getBuckets().add("3");
					oneReport.getBuckets().add("5");
					oneReport.getBuckets().add("10");
					oneReport.getBuckets().add("20");
				} else {
					oneReport.setBuckets(calcBody.getResultBucketParam());
				}

				calcReports.add(oneReport);
			}
		} else if (CtCalcConsts.UNIT_TYPE_ALL.equals(unit)) {

			CtAddictionsTimesReport oneReport = this.copyBody(calcBody);
			oneReport.setTheCalcFromDate(calcBody.getFromDate());
			oneReport.setTheCalcToDate(calcBody.getToDate());

			if (calcBody.getResultBucketParam().isEmpty()) {
				oneReport.getBuckets().add("5");
				oneReport.getBuckets().add("10");
				oneReport.getBuckets().add("20");
				oneReport.getBuckets().add("50");
			} else {
				oneReport.setBuckets(calcBody.getResultBucketParam());
			}

			calcReports.add(oneReport);
		}

		log.info("calcReports={}", JSON.toJSONString(calcReports));
		return calcReports;

	}

	public CtAddictionsTimesReport copyBody(CtAddictionsCalcBody body) {
		CtAddictionsTimesReport oneReport = new CtAddictionsTimesReport();

		// CtgApplicationInfo
		oneReport.setAppId(body.getAppId());
		oneReport.setBookmarkId(body.getBookmarkId());
		oneReport.setRequestId(body.getRequestId());

		// CtCalcCommonBody
		oneReport.setFromDate(body.getFromDate());
		oneReport.setToDate(body.getToDate());
		oneReport.setUnit(body.getUnit());

		oneReport.setSubjectId(body.getSubjectId());
		oneReport.setSubTaskType(body.getSubTaskType());
		oneReport.setFilter(body.getFilter());

		// CtAddictionsCalcBody
		oneReport.setEventId(body.getEventId());
		oneReport.setMeasure(body.getMeasure());
		oneReport.setMeasureType(body.getMeasureType());
		oneReport.setUserFilter(body.getUserFilter());
		oneReport.setByField(body.getByField());
		oneReport.setResultBucketParam(body.getResultBucketParam());

		return oneReport;
	}

}
